package com.situ.day2;

import org.junit.Test;

import java.util.Scanner;

public class Demo {

    @Test
    public void test1() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入字符串： ");
        String str = scanner.next();
        //String str = "abcba";
        char[] array = str.toCharArray();
        //初始认为是回文,接下来遍历能不能找到一个反例
        //遍历到最后都没有找到这个反例，代表是回文
        boolean isHuiWen = true;
        for (int i = 0; i < array.length / 2; i++) {
            if (array[i] != array[array.length - 1 - i]) {
                isHuiWen = false;
                break;
            }
        }
        if (isHuiWen) {
            System.out.println("是回文");
        } else {
            System.out.println("不是回文");
        }
    }

    @Test
    public void test11() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入字符串： ");
        String str = scanner.next();
        boolean isHuiWen = isHuiWen(str);
        if (isHuiWen) {
            System.out.println("是回文");
        } else {
            System.out.println("不是回文");
        }
    }

    private boolean isHuiWen(String str) {
        if (str == null || str.length() == 0) {
            throw new RuntimeException();
        }
        //String str = "abcba";
        char[] array = str.toCharArray();
        //初始认为是回文,接下来遍历能不能找到一个反例
        //遍历到最后都没有找到这个反例，代表是回文
        boolean isHuiWen = true;
        for (int i = 0; i < array.length / 2; i++) {
            if (array[i] != array[array.length - 1 - i]) {
                isHuiWen = false;
                break;
            }
        }
        return isHuiWen;
    }
}
